/**
 * Created by meiguizhinian on 2018/7/19
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIProgressBar = ivy.UIBase.extend({
	/**
	 * UIProgressBar(formName, blockName, sourceControlData);
	 * //带追踪条初始化
	 * UIProgressBar(texName, trackTime, percent=100.0, baseBarColor=cc.color.WHITE, trackBarColor=cc.color.GRAY, dir=ivy.UIProgressBar.Direction.eLeft);
	 * */
	ctor: function () {
		this._super();
		this._dir = ivy.UIProgressBar.Direction.eLeft;
		this._baseBarColor = cc.color.WHITE;
		this._trackBarColor = cc.color.GRAY;
		if (arguments.length!==0) {
			if (cc.isNumber(arguments[1])) {
				let texName = arguments[0];
				let trackTime = arguments[1];
				let percent = arguments.length>2 ? arguments[2]:100.0;
				let baseBarColor = arguments.length>3 ? arguments[3]:cc.color.WHITE;
				let trackBarColor = arguments.length>4 ? arguments[4]:cc.color.GRAY;
				let dir = arguments.length>5 ? arguments[5]:ivy.UIProgressBar.Direction.eLeft;

				this._baseLoadingBar = new ccui.LoadingBar(texName);
				this._baseBarColor = baseBarColor;
				this._baseLoadingBar.setColor(this._baseBarColor);
				this.addChild(this._baseLoadingBar);

				this._trackLoadingBar = new ccui.LoadingBar(texName);
				this._trackBarColor = trackBarColor;
				this._trackLoadingBar.setColor(this._trackBarColor);
				this.addChild(this._trackLoadingBar,-1);
				this._trackTime = trackTime;

				this.setCascadeColorAndOpacityEnabled(true);
				this.setDirection(dir);
				this.setPercent(percent,true);
				this.scheduleUpdate();
			} else {
				//formName, blockName, sourceControlData
				this.initWith.apply(this, arguments);
			}
		}
	},

	/**
	 * initWith(formID, blockID, texName, percent=100.0, baseBarColor=cc.color(255,255,255), dir=ivy.UIProgressBar.Direction.eLeft);
	 * initWith(texName, percent=100.0, baseBarColor=cc.color(255,255,255), dir=ivy.UIProgressBar.Direction.eLeft);
	 * initWith(formName, blockName, sourceControlData);
	 * */
	initWith : function() {
		if (cc.isNumber(arguments[0])) {
			let formID = arguments[0];
			let blockID = arguments[1];
			let texName = arguments[2];
			let percent = arguments.length>3 ? arguments[3]:100.0;
			let baseBarColor = arguments.length>4 ? arguments[4]:cc.color.WHITE;
			let dir = arguments.length>5 ? arguments[5]:ivy.UIProgressBar.Direction.eLeft;

			this.initWith(texName,percent,baseBarColor,dir);
			let uim = ivy.UIManager.getInstance();
			let x = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.ePosX);
			let y = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.ePosY);
			let w = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eWidth);
			let h = uim.getBlockDataByType(formID, blockID, ivy.UIManager.FormDataType.eHeight);
			this.setSrcRect(cc.rect(x, y, w, h));
			this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
			this.setContentSize(cc.size(w, h));
		} else {
			if (cc.isNumber(arguments[1])) {
				let texName = arguments[0];
				let percent = arguments.length>1 ? arguments[1]:100.0;
				let baseBarColor = arguments.length>2 ? arguments[2]:cc.color.WHITE;
				let dir = arguments.length>3 ? arguments[3]:ivy.UIProgressBar.Direction.eLeft;

				if (!this._baseLoadingBar) {
					this._baseLoadingBar = new ccui.LoadingBar(texName);
					this._baseLoadingBar.setColor(baseBarColor);
					this.addChild(this._baseLoadingBar);
				}
				this.setCascadeColorAndOpacityEnabled(true);
				this.setDirection(dir);
				this.setPercent(percent,true);
				cc.Sprite.create()
			} else {
				let formName = arguments[0];
				let blockName = arguments[1];
				let sourceControlData = arguments[2];

				this.init();
				let uim = ivy.UIManager.getInstance();
				let aniIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgAniID);
				let actionIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgActionID);
				let seqIDInUI = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eBgSeqID);
				let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
				let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
				let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
				let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
				let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
				this._layerIDInEditor = layerID;
				if (aniIDInUI >= 0) {
					let aniFileName = ivy.UIManager.getInstance().getAniFileNameByUIAniID(aniIDInUI);
					this._backAniPlayer = ivy.AniPlayer.create(aniFileName, actionIDInUI, seqIDInUI === -1 ? 0 : seqIDInUI, true, seqIDInUI === -1 ? ivy.AniPlayState.ePause : ivy.AniPlayState.eCircle);
					this.addChild(this._backAniPlayer);
				}
				this.setLocalZOrder(layerID);
				this.setSrcRect(cc.rect(x, y, w, h));
				this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
				this.setSrcPosition(this.getPosition());
				this.setContentSize(cc.size(w, h));
				let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_AniBox_Anchor_list]);
				this.setAnchorType(anchorType);
				this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_EnterAction_action]));
				this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_OutAction_action]));

				let mainPicPath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_MainPicPath_string]);
				let mainColor = ivy.Tools.getColor4BFromInt32(uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_MainColor_color]));
				let defaultPercent = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_DefaultPercent_int]);
				let dir = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_Dir_list]);
				let offsetX = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_XOffset_int]);
				let offsetY = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_YOffset_int]);

				this._useRadialShowType= uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_UseRadialShowType_list]);
				let radialMinPointX= uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_MidPoint_X_float]);
				let radialMinPointY = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_MidPoint_Y_float]);

				let trackPicPath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_TrackPicPath_string]);
				let trackColor = ivy.Tools.getColor4BFromInt32(uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_TrackColor_color]));
				let trackTime = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_TrackTime_int]);

				if (this._useRadialShowType && mainPicPath.length>0) {
					if (!this._baseRadialLoadingBar) {
						let s = new cc.Sprite(mainPicPath);
						this._baseRadialLoadingBar = new cc.ProgressTimer(s);
						this._baseBarColor = cc.color(mainColor);
						this._baseRadialLoadingBar.setOpacity(mainColor.a);
						this._baseRadialLoadingBar.setColor(this._baseBarColor);
						this._baseRadialLoadingBar.setCascadeOpacityEnabled(true);
						this._baseRadialLoadingBar.setCascadeColorEnabled(true);
						this._baseRadialLoadingBar.setPosition(cc.p(offsetX, offsetY));
						this.addChild(this._baseRadialLoadingBar);
					}
					let isShowTrack = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_ShowTrackBar_list]) === 1;
					if (isShowTrack) {
						if (!this._trackRadialLoadingBar) {
							this._trackRadialLoadingBar = new cc.ProgressTimer(new cc.Sprite(trackPicPath));
							this._trackBarColor = cc.color(trackColor);
							this._trackRadialLoadingBar.setOpacity(trackColor.a);
							this._trackRadialLoadingBar.setColor(this._trackBarColor);
							this._trackRadialLoadingBar.setCascadeOpacityEnabled(true);
							this._trackRadialLoadingBar.setCascadeColorEnabled(true);
							this.addChild(this._trackRadialLoadingBar, -1);
							this._trackTime = trackTime;
							this.scheduleUpdate();
						}
					}
				} else {
					if (!this._baseLoadingBar) {
						this._baseLoadingBar = new ccui.LoadingBar(mainPicPath);
						this._baseBarColor = cc.color(mainColor);
						this._baseLoadingBar.setOpacity(mainColor.a);
						this._baseLoadingBar.setColor(this._baseBarColor);
						this._baseLoadingBar.setPosition(cc.p(offsetX, offsetY));
						this.addChild(this._baseLoadingBar);
					}
					let isShowTrack = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_ShowTrackBar_list]) === 1;
					if (isShowTrack) {
						if (!this._trackLoadingBar) {
							this._trackLoadingBar = new ccui.LoadingBar(trackPicPath);
							this._trackBarColor = cc.color(trackColor);
							this._trackLoadingBar.setOpacity(trackColor.a);
							this._trackLoadingBar.setColor(this._trackBarColor);
							this.addChild(this._trackLoadingBar, -1);
							this._trackTime = trackTime;
							this.scheduleUpdate();
						}
					}
				}

				let showUpBackAniplayer= uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ProgressBar_UpShow_list]) === 1;
				if (this._backAniPlayer) {
					if (showUpBackAniplayer) {
						this._backAniPlayer.setLocalZOrder(-2);
					} else {
						this._backAniPlayer.setLocalZOrder(1);
					}
				}
				this.setDirection(dir);
				this.setPercent(defaultPercent,true);
				this.setUIElementName(formName, blockName);
				//////////////////////////////////////////////////////////////////////////
				//设置初始可视性 wsq-2017-3-7
				let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
				if (isNotVisble === 1) {
					this.setVisible(false);
				}
			}
		}
	},

	update : function(dt) {
		if (this._refreshFunc) {
			this._refreshFunc(this);
		}
		if (this._trackTimer > 0) {
			--this._trackTimer;
			let p = ivy.Tools.lerp(this._percent, this._trackLastPercent, this._trackTimer, this._trackTime);
			if (this._useRadialShowType) {
				this._trackRadialLoadingBar.setPercentage(p);
			} else {
				this._trackLoadingBar.setPercent(p);
			}
		}
	},

	//设置进度条的百分比
	/**
	 * void setPercent(float p,bool resetTrackPercent);
	 * void setPercent(float curV, float max, bool resetTrackPercent = false);
	 */
	setPercent : function() {
		let p = null;
		let resetTrackPercent = false;
		if (typeof arguments[1] === "number") {
			p = arguments[0]*100/arguments[1];
			if (arguments.length>2) {
				resetTrackPercent = arguments[2];
			}
		} else {
			p = arguments[0];
			resetTrackPercent = arguments[1];
		}
		if (this._percent === p) {
			return;
		}
		this._percent = p;
		if (this._useRadialShowType) {
			if (this._baseRadialLoadingBar)
				this._baseRadialLoadingBar.setPercentage(p);
			if (this._trackRadialLoadingBar) {
				this._trackTimer = this._trackTime;
				if (resetTrackPercent)
					this._trackLastPercent = p;
				else
					this._trackLastPercent = this._trackRadialLoadingBar.getPercentage();
			}
		} else {
			if (this._baseLoadingBar)
				this._baseLoadingBar.setPercent(p);
			if (this._trackLoadingBar) {
				this._trackTimer = this._trackTime;
				if (resetTrackPercent)
					this._trackLastPercent = p;
				else
					this._trackLastPercent = this._trackLoadingBar.getPercent();
			}
		}
	},
	getPercent : function() {
		return this._percent;
	},

	//设置进度条的方向
	setDirection : function(dir) {
		this._dir = dir;
		if (this._useRadialShowType) {
			if (this._baseRadialLoadingBar) {
				this._baseRadialLoadingBar.setReverseProgress(this._dir!==ivy.UIProgressBar.Direction.eLeft);
			}
			if (this._trackRadialLoadingBar) {
				this._trackRadialLoadingBar.setReverseProgress(this._dir!==ivy.UIProgressBar.Direction.eLeft);
			}
		} else {
			if (this._baseLoadingBar) {
				this._baseLoadingBar.setDirection(this._dir);
			}
			if (this._trackLoadingBar) {
				this._trackLoadingBar.setDirection(this._dir);
			}
		}
	},
	getDirection : function() {
		return this._dir;
	},

	//调整进度条的显示大小
	resetBarSize : function(s) {
		if (this._useRadialShowType) {
			if (this._baseRadialLoadingBar) {
				let size = this._baseRadialLoadingBar.getSprite().getTexture().getContentSizeInPixels();
				this._baseRadialLoadingBar.setScale(s.width/size.width,s.height/size.height);
			}
			if (this._trackRadialLoadingBar) {
				let size = this._trackRadialLoadingBar.getSprite().getTexture().getContentSizeInPixels();
				this._trackRadialLoadingBar.setScale(s.width / size.width, s.height / size.height);
			}
		} else {
			if (this._baseLoadingBar) {
				this._baseLoadingBar.setScale9Enabled(true);
				this._baseLoadingBar.setContentSize(s);
			}
			if (this._trackLoadingBar) {
				this._trackLoadingBar.setScale9Enabled(true);
				this._trackLoadingBar.setContentSize(s);
			}
		}
	},
	//设置监听函数
	setRefreshFunc : function(func) {
		this._refreshFunc = func;
		if (func) {
			this.scheduleUpdate();
		} else {
			this.unscheduleUpdate();
		}
	},

	updateDisplayedOpacity : function(parentOpacity) {
		this._super(parentOpacity);
		if (this._baseLoadingBar) {
			this._baseLoadingBar.setOpacity(parentOpacity);
		}
		if (this._trackLoadingBar) {
			this._trackLoadingBar.setOpacity(parentOpacity);
		}
		if (this._baseRadialLoadingBar) {
			this._baseRadialLoadingBar.setOpacity(parentOpacity);
		}
		if (this._trackRadialLoadingBar) {
			this._trackRadialLoadingBar.setOpacity(parentOpacity);
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_baseLoadingBar : null,//cocos2d::ui::LoadingBar*
	_trackLoadingBar : null,//cocos2d::ui::LoadingBar*
	_baseRadialLoadingBar : null,//cocos2d::ProgressTimer*
	_trackRadialLoadingBar : null,//cocos2d::ProgressTimer*
	_percent : 0,//float
	_trackLastPercent : 0,//float
	_dir  : 0,//Direction::eLeft
	_trackTime : 0,//int32_t
	_trackTimer : 0,//int32_t
	_baseBarColor : null,//cocos2d::Color3B::WHITE
	_trackBarColor : null,//cocos2d::Color3B::WHITE
	_refreshFunc : null,//std::function<void(UIProgressBar*)>
	_useRadialShowType : false,
	_backAniPlayer : null,//cc::AniPlayer*
});

ivy.UIProgressBar.create = function (...args) {
	return new ivy.UIProgressBar(...args);
};

ivy.UIProgressBar.Direction = {
	eLeft   : 0,
	eRight  : 1,
};